package ywpt.service.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import ywpt.core.dao.HibernateEntityDao;
import ywpt.model.ClientDevice;
import ywpt.model.ClientInfo;
import ywpt.model.ClientKeshi;
import ywpt.model.ProviderInfo;
import ywpt.model.ShebeiType;
import ywpt.web.common.PublicFunction;

public class ClientDeviceServiceImpl extends HibernateEntityDao<ClientDevice> implements ClientDeviceService{
	public ClientDeviceServiceImpl(){
		
	}

	
	@Override
	public String createDeviceCode() {
		DetachedCriteria query = DetachedCriteria.forClass(ClientDevice.class, "device");
		String deviceCode="";
		String code="";
		Integer maxId=new Integer(0);
		query.addOrder(Order.desc("id"));
		List<ClientDevice> clientDevices = this.getClientDevices(query);
		if ( clientDevices.iterator().hasNext() )	
		{
			code = clientDevices.iterator().next().getCode();
			maxId = new Integer(code);
		}
		deviceCode=PublicFunction.getNextCode("", maxId, false,4); 
		query.setProjection(null);
		return deviceCode;
	}

	@Override
	public ClientDevice getClientDeviceById(Long id) {
		return this.get(id);
	}

	@Override
	public int getClientDeviceCountByCriteria(DetachedCriteria query) {
		return this.getCountByCriteria(query);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientDevice> getClientDevices(DetachedCriteria query,
			int pageSize, int startIndex) {
		return this.findAllByCriteria(query, pageSize, startIndex);
	}

	@Override
	public List<ClientDevice> getClientDevices() {
		return this.getAll();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<ClientDevice> getClientDevices(DetachedCriteria query) {
		return this.findAllByCriteria(query);
	}

	@Override
	public void removeClientDevice(Long id) {
		this.removeById(id);
	}

	@Override
	public void saveClientDevice(ClientDevice clientDevice, Long typeId,
			Long clientId, Long keshiId, Long providerId) {
		if(typeId != null){
			clientDevice.setType(this.get(ShebeiType.class, typeId));
		}
		if( clientId != null){
			clientDevice.setClient(this.get(ClientInfo.class, clientId));
		}
		if(keshiId != null){
			clientDevice.setKeshi(this.get(ClientKeshi.class, keshiId));
		}
		if(providerId != null){
			clientDevice.setProvider(this.get(ProviderInfo.class, providerId));
		}
		super.save(clientDevice);
	}

	@Override
	public void updateClientDevice(ClientDevice clientDevice, Long typeId,
			Long clientId, Long keshiId, Long providerId) {
		if(typeId != null){
			clientDevice.setType(this.get(ShebeiType.class, typeId));
		}
		if( clientId != null){
			clientDevice.setClient(this.get(ClientInfo.class, clientId));
		}
		if(keshiId != null){
			clientDevice.setKeshi(this.get(ClientKeshi.class, keshiId));
		}
		if(providerId != null){
			clientDevice.setProvider(this.get(ProviderInfo.class, providerId));
		}
		super.update(clientDevice);
	}


	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<ClientDevice> getDeviceByDeviceName(String deviceName,Long clientId,Long keshiId,
			int start, int size) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientDevice.class);
		if (clientId!=null) {
			query.createAlias("client", "client").add(Restrictions.eq("client.id", clientId));
		}
		if (keshiId!=null) {
			query.createAlias("keshi", "keshi").add(Restrictions.eq("keshi.id", keshiId));
		}
		if ( deviceName != null && !deviceName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + deviceName + "%"),  
							          Restrictions.like("code", "%" + deviceName + "%")));
		}
		query.addOrder(Order.asc("code"));
		List<ClientDevice> clientDeviceList = new ArrayList<ClientDevice>();
		List<ClientDevice> clientDeviceLists = new ArrayList<ClientDevice>();
		ClientDevice device =new ClientDevice();
		
		clientDeviceList.addAll(this.findAllByCriteria(query, size, start));
		int i = 1;
		for ( Iterator<ClientDevice> itr = clientDeviceList.iterator(); itr.hasNext(); i++ ) 
		{
			device = itr.next();
			String ss = PublicFunction.format(device.getCode() + "", 7, "  ", 1)
			          + device.getName();
			
			device.setName(ss);
			clientDeviceLists.add(device);
		}
		return clientDeviceLists;
	}


	@Override
	public int getDeviceCountByName(String deviceName,Long clientId,Long keshiId) {
		DetachedCriteria query = DetachedCriteria.forClass(ClientDevice.class);
		if (clientId!=null) {
			query.createAlias("client", "client").add(Restrictions.eq("client.id", clientId));
		}
		if (keshiId!=null) {
			query.createAlias("keshi", "keshi").add(Restrictions.eq("keshi.id", keshiId));
		}
		if ( deviceName != null && !deviceName.trim().equals("") )
		{
			query.add(Restrictions.or(Restrictions.like("name", "%" + deviceName + "%" ),  
							          Restrictions.like("code", "%" + deviceName + "%" )));
		}
		query.setProjection(null);
		return this.getCountByCriteria(query);
	}
}
